/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#ifndef INCLUDE_NET_NET_REACTOR_H_
#define INCLUDE_NET_NET_REACTOR_H_

#include "netbasedefine.h"

class net_reactor_impl_t; class IEvent_handler;
class net_reactor_t
{
	net_reactor_t( const net_reactor_t& );
	net_reactor_t& operator= ( const net_reactor_t& );
public:
	net_reactor_t();
	~net_reactor_t();

	int init_reactor( IEvent_handler* pHandler, int threadnum = 4 );
	int uninit_reactor();
public:
	SOCK_FD open_tcp_server( const char* local_ip, int local_port );
	SOCK_FD open_tcp_client( const char* remote_ip, int remote_port );
	SOCK_FD open_udp_endpoint( const char* local_ip, int local_port );

public:
	int send( SOCK_FD fd, const char* data, int len );
	int sendto( SOCK_FD fd, const char* data, int len, const char* remote_ip, int remote_port );
	int close_fd( SOCK_FD fd );

private:
	net_reactor_impl_t* m_impl;
};

class IEvent_handler
{
public:
	virtual ~IEvent_handler() { }
public:
	virtual int on_connect( SOCK_FD listen_fd, SOCK_FD cli_fd, const char* remote_ip, int remote_port ) = 0;
	virtual int on_data( SOCK_FD fd, const char* data, int len, const char* remote_ip, int remote_port ) = 0;
	virtual int on_send_ack( SOCK_FD fd, int len ) = 0;
	virtual int on_close( SOCK_FD fd, int error ) = 0;
};


#endif //~INCLUDE_NET_NET_REACTOR_H_
